/*
 * Copyright 2008 Carnegie Mellon University
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package edu.cmu.lti.sepia.task.retrieval;

import edu.cmu.lti.sepia.data.Lang;
import edu.cmu.lti.sepia.task.nlp.japanese.NLPTool;

public class SimpleQueryFormulator {

  private static NLPTool nlpTool;

  // private static SimplifiedChineseSegmenter scs;
  // private static Segmenter scs;
  // private static Segmenter tcs;
  private static boolean initDone = false;

  private static void init() {
    try {
      nlpTool = new NLPTool();
      // scs = new SimplifiedChineseSegmenter();
      // scs = new Segmenter(Segmenter.SIMP, true);
      // tcs = new Segmenter(Segmenter.TRAD, true);
      initDone = true;
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static String segmentCS(String sentence) {
    // if (!initDone) init();
    // //return scs.segment( sentence );
    // return scs.segmentLine( sentence," ");
    return sentence;
  }

  public static String segmentCT(String sentence) {
    // if (!initDone) init();
    // return tcs.segmentLine( sentence," ");
    return sentence;
  }

  public static String segmentJP(String sentence) {
    if (!initDone)
      init();
    return nlpTool.segment(sentence);
  }

  public static String segment(String lang, String sentence) {
    String result = sentence;
    if (lang.matches("jp|JP|ja|JA")) {
      result = segmentJP(sentence);
    } else if (lang.matches("cs|CS")) {
      result = segmentCS(sentence);
    } else if (lang.matches("ct|CT")) {
      result = segmentCT(sentence);
    }
    return result;
  }

  public static String segmentByChar(String sentence) {
    String[] chars = sentence.split("");
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < chars.length; i++) {
      sb.append((i > 1) ? " " : "");
      sb.append((i > 0) ? (chars[i]) : "");
    }
    return sb.toString();
  }

//  private static String formulate(String lang, String q) {
//    q = q.replaceAll("[\\s]+$", "");
//    q = q.replaceAll("^[\\s]+", "");
//    q = q.replaceAll("　", " ");
//
//    String[] words = q.split(" ");
//
//    StringBuilder sb = new StringBuilder();
//
//    for (int i = 0; i < words.length; i++) {
//      String phrase = segment(lang, words[i]);
//      sb.append((i > 0) ? " " : "");
//      sb.append(enclosePhrase(phrase));
//    }
//
//    return encloseCombine(sb.toString());
//  }

  /**
   * 
   * @param q query words
   * @param lang language
   * @param queryType one of OR, AND and PHRASE
   * @return indri query
   */
  public static String formulateQuery(String q, String langString, String queryType) {
    Lang lang = Lang.valueOf(langString.toUpperCase());
    boolean segmentByChar = !lang.isSpaceSeparatedLanguage(); 
    return formulateQuery(q, segmentByChar, queryType);
  }
  
  private static String formulateQuery(String q, boolean segmentByChar, String queryType) {
    q = q.trim();
    q = q.replaceAll("　", " ");

    String[] words = q.split("[ ]+");

    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < words.length; i++) {
      String phrase = segmentByChar ? segmentByChar(words[i]) : words[i];
      sb.append((i > 0) ? " " : "");
      sb.append(enclosePhrase(phrase));
    }
System.out.println(queryType);
    if ("AND".equals(queryType)) {
      return "#uw( " + sb.toString() + " )";
    } else if ("PHRASE".equals(queryType)) {
      return "#1( " + sb.toString() + " )";
    } else {//including null queryType
      return "#combine( " + sb.toString() + " )";
    }
  }

  private static String enclosePhrase(String s) {
    // return s.length() == 1 ? (s) : ("#1( "+ s +" )");
    return "#1( " + s + " )";
  }

  public static void main(String[] args) {
    String jp = "今日はいい天気です";
//    String ct = "江澤民：台灣問題是中美最重要問題";
//    String cs = "从列表选择一个问题";

    System.out.println(jp);
    System.out.println(SimpleQueryFormulator.segmentJP(jp));
    // System.out.println(ct);
    // System.out.println(SimpleQueryFormulator.segmentCT(ct));
    // System.out.println(cs);
    // System.out.println(SimpleQueryFormulator.segmentCS(cs));
  }

}
